'use strict';

var fs = require('fs');
var path = require('path');
var request = require('request');
var co = require('co');
var file = path.join(__dirname, 'url.txt');

function getUrl() {
    console.log('getUrl');
    return new Promise(function(fullfil, reject) {
        fs.readFile(file, 'utf8', function(err, res) {
            if (err) reject(err);
            else {
                console.log('url=' + res);
                fullfil(res);
            }
        });
    });
}

function makeRequest(url) {
    console.log('makeRequest,url=' + url);
    // url = 'http://haha.foo.bar.com';
    return new Promise(function(fullfil, reject) {
        request(url, function(err, res, body) {
            if (err) reject(err);
            else {
                console.log('makeRequest ok');
                fullfil(body);
            }
        });
    });
}


function* main() {
    console.log('getUrl start');
    var url = yield getUrl();
    console.log('makeRequest start');
    var body = yield makeRequest(url);
    console.log(body);
}

co(main);